package com.grep.extra.factory.handle.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.grep.base.util.FilePathUtil;
import com.grep.base.util.UUIDGenerator;
import com.grep.extra.dao.ContentDao;
import com.grep.extra.dao.impl.ContentDAOSQL;
import com.grep.extra.factory.data.IDataQueue;
import com.grep.extra.factory.data.impl.PageContent;
import com.grep.extra.factory.data.impl.TaskContentUrl;
import com.grep.extra.factory.handle.IHandler;
import com.grep.extra.factory.utils.JsoupUtil;

public class ContentUrlHandler implements IHandler <TaskContentUrl,PageContent> {

	static Logger logger = LoggerFactory.getLogger(ContentUrlHandler.class);
	
	private IDataQueue<TaskContentUrl> task = null ;
	
	private IDataQueue<PageContent> data = null ;
	
	private ContentDao contentDao = new ContentDAOSQL() ;
	
	public IDataQueue<TaskContentUrl> getTask() {
		return task;
	}

	//通过URL初始化数据，如采集状态，采集策略等
	public void init(IDataQueue<TaskContentUrl> task,IDataQueue<PageContent> data) {
		this.task = task ;
		this.data = data ;
	}

	public List<PageContent> process(TaskContentUrl url) {
		logger.debug("[ContentUrlHandler] handler start !");
		if(url.getId() == null){
			url.setId(UUIDGenerator.getUUID()) ;
		}
		writeContentFile(url) ;
		
		logger.debug("[ContentUrlHandler] handler end !");
		return null;
	}
	
	/**
	 * 根据URL本地写URL内容到本地
	 * @param url
	 */
	private void writeContentFile(TaskContentUrl url){
		//写文件
		FilePathUtil.mkdir(url.getPath()) ;
		JsoupUtil jutil = null ;
		try{
			jutil = new JsoupUtil(url.getTaskContentUrl()) ;
			url.setPath(url.getPath()+"//"+url.getId()+".html") ;
			jutil.writeContentFile( url.getPath()) ;
		}catch(Exception e){
			e.printStackTrace() ;
			return ;
		}
		
		//生成页面对象交给下个流程处理
		PageContent content = new PageContent() ;
		if(jutil !=null ){
			content.setCharset(jutil.getCharset());
		}
		content.setTaskId(url.getTaskId());
		content.setPath(url.getPath()) ;
		this.data.put(content) ;
	}

}
